<?php
include("../lib/functions.php");
noCache();
session_start();
?>
<?php require_once('../../Connections/cotiza.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_cotiza, $cotiza);/*
$query_articulos = "SELECT * FROM pt_mstr";
$articulos = mysql_query($query_articulos, $cotiza) or die(mysql_error());
$row_articulos = mysql_fetch_assoc($articulos);
$totalRows_articulos = mysql_num_rows($articulos);*/

$query_grupos 	= "SELECT * FROM pt_group_view";
$grupos			= mysql_query($query_grupos,$cotiza);
$row_grupos		= mysql_fetch_assoc($grupos);

$query_materiales = "SELECT * FROM mat_mstr WHERE mat_tipo <> 'L' ORDER BY mat_id";
$materiales = mysql_query($query_materiales, $cotiza) or die(mysql_error());
$row_materiales = mysql_fetch_assoc($materiales);
$totalRows_materiales = mysql_num_rows($materiales);
?>
<?php require_once('../../include/defaults.php'); ?>
<?php require_once("../../include/language.php");?>
<?php

$acc = $_GET['acc'];
$c_m = $_GET['cm'];

mysql_select_db($database_cotiza, $cotiza);

if($acc == "edit" and $_GET['cod_material'] != "")
{
	$cod_material   = strtoupper($_GET['cod_material']);
	$desc_material	= $_POST['desc_material'];
	
	$query = "SELECT * FROM mat_mstr WHERE mat_id = '$cod_material'";
	$registros = mysql_query($query, $cotiza) or die(mysql_error());
	$row = mysql_fetch_assoc($registros);
	$totalRows = mysql_num_rows($registros);
	
	$query_capas = "SELECT * FROM mat_capa WHERE matc_mat_id = '$cod_material'";
	$registros_capas = mysql_query($query_capas, $cotiza) or die(mysql_error());
	$porcentaje_capas = mysql_query($query_capas, $cotiza);
	$row_capas = mysql_fetch_assoc($registros_capas);
	$c = mysql_num_rows($registros_capas);
	
	$query_comp = "SELECT * FROM mat_comprado WHERE matco_mat_id = '$cod_material'";
	$registros_comp = mysql_query($query_comp, $cotiza) or die(mysql_error());
	$row_comp = mysql_fetch_assoc($registros_comp);
	
	$query_lam 		= "SELECT * FROM mat_laminado WHERE matl_mat_id = '$cod_material' ORDER BY matl_id";
	$registros_lam 	= mysql_query($query_lam, $cotiza) or die(mysql_error());
	$row_lam 		= mysql_fetch_assoc($registros_lam);
	$tot_row_lam   	= mysql_num_rows($registros_lam);
	$i = 0;
	
	do{
		$lam[$i] = $row_lam['matl_id_hijo'];
		$i++;
	} while ($row_lam = mysql_fetch_assoc($registros_lam));
	
	$desc_material = $row['mat_desc'];
	$tipo_material = $row['mat_tipo'];
	
	if($tipo_material == "")
		$tipo_material = "E";
}


if ($acc == "save")
{
	$tipo_material  = $_POST['tipo_material'];
	$mat_id			= strtoupper($_POST['cod_material']);
	$mat_desc		= $_POST['desc_material'];
	$id_capas		= $_POST['id_capa'];
	$pct_capa		= $_POST['pct_capa'];
	$secuencia		= asigna_secuencia("mat_mstr","mat_id_qad",$cotiza,2);
	
	if($tipo_material == "E")
	{
		$query = "INSERT INTO mat_mstr(mat_id, mat_id_qad, mat_desc, mat_tipo, mat_completo) VALUES ('$mat_id', '$secuencia', '$mat_desc', 'E', 0) ON DUPLICATE KEY UPDATE mat_desc = '$mat_desc', mat_tipo = 'E', mat_completo = 0";
		mysql_query($query,$cotiza) or die (mysql_error());
		
		$query = "DELETE FROM mat_capa WHERE matc_mat_id = '$mat_id'";
		mysql_query($query,$cotiza) or die (mysql_error());
		
		$query = "DELETE FROM mat_laminado WHERE matl_mat_id = '$mat_id'";
		mysql_query($query,$cotiza) or die (mysql_error());
		
		$query = "DELETE FROM mat_comprado WHERE matco_mat_id = '$mat_id'";
		mysql_query($query,$cotiza) or die (mysql_error());		
		
		$i = 0;/*
		foreach($pct_capa as $pcc)
		{
			$pc[$i] = $pcc;
			$i++;
		}
		
		$i=0;
		foreach($id_capas as $idc)
		{
			$query = "INSERT INTO mat_capa (matc_mat_id,matc_capa,matc_pct_espesor) 
			          VALUES ('$mat_id', $idc, $pc[$i])";
			mysql_query($query,$cotiza);
			$i++;
		}*/
		//echo $tipo_material;
		//header("Location: mntomateriales_2.php?mat_id=$mat_id");
		header("Location: mntomateriales.php");
	}
	if($tipo_material == "C")
	{
		$part		= $_POST['part'];
		$micronaje	= $_POST['micronaje'];
		
		if($micronaje == "")
			$micronaje = 0;
			
		$query = "INSERT INTO mat_mstr(mat_id, mat_id_qad, mat_desc, mat_tipo, mat_completo) VALUES ('$mat_id', '$secuencia', '$mat_desc', 'C', 1) ON DUPLICATE KEY UPDATE mat_desc = '$mat_desc', mat_tipo = 'C', mat_completo = 0";
		mysql_query($query,$cotiza) or die (mysql_error());
		
		$query = "DELETE FROM mat_capa WHERE matc_mat_id = '$mat_id'";
		mysql_query($query,$cotiza);
		
		$query = "DELETE FROM matcd_det WHERE matcd_mat_id = '$mat_id'";
		mysql_query($query,$cotiza);
		
		$query = "DELETE FROM mat_laminado WHERE matl_mat_id = '$mat_id'";
		mysql_query($query,$cotiza);
		
		$query = "INSERT INTO mat_comprado(matco_pt_part_type, matco_mat_id) VALUES('$part', '$mat_id') 
	             ON DUPLICATE KEY UPDATE matco_pt_part_type = '$part'";
		mysql_query($query,$cotiza) or die(mysql_error());
	}
	
	if($tipo_material == "L")
	{
		$query = "INSERT INTO mat_mstr(mat_id, mat_id_qad, mat_desc, mat_tipo, mat_completo) VALUES ('$mat_id', '$secuencia', '$mat_desc', 'L', 0) ON DUPLICATE KEY UPDATE mat_desc = '$mat_desc', mat_tipo = 'L', mat_completo = 0";
		mysql_query($query,$cotiza) or die (mysql_error());
		
		$query = "DELETE FROM mat_capa WHERE matc_mat_id = '$mat_id'";
		mysql_query($query,$cotiza) or die (mysql_error());
		
		$query = "DELETE FROM mat_laminado WHERE matl_mat_id = '$mat_id'";
		mysql_query($query,$cotiza) or die (mysql_error());
		
		$query = "DELETE FROM mat_comprado WHERE matco_mat_id = '$mat_id'";
		mysql_query($query,$cotiza) or die (mysql_error());
		
		$lamina1 = $_POST['lamina1'];
		$lamina2 = $_POST['lamina2'];
		$lamina3 = $_POST['lamina3'];
		$lamina4 = $_POST['lamina4'];
		
		if($lamina1 != "")
		{
			$query = "INSERT INTO mat_laminado(matl_id,matl_mat_id,matl_id_hijo) VALUES (1,'$mat_id','$lamina1')";	
			mysql_query($query,$cotiza) or die (mysql_error());
		}
		
		if($lamina2 != "")
		{
			$query = "INSERT INTO mat_laminado(matl_id,matl_mat_id,matl_id_hijo) VALUES (2,'$mat_id','$lamina2')";	
			mysql_query($query,$cotiza) or die (mysql_error());
		}
		
		if($lamina3 != "")
		{
			$query = "INSERT INTO mat_laminado(matl_id,matl_mat_id,matl_id_hijo) VALUES (3,'$mat_id','$lamina3')";	
			mysql_query($query,$cotiza) or die (mysql_error());
		}
		
		if($lamina4 != "")
		{
			$query = "INSERT INTO mat_laminado(matl_id,matl_mat_id,matl_id_hijo) VALUES (4,'$mat_id','$lamina4')";	
			mysql_query($query,$cotiza) or die (mysql_error());
		}
		
		header("Location: mntomateriales.php");
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>..:: MNTO Materiales ::..</title>
<link href="../../main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../lib/funciones.js"></script>
<script language="javascript">
function enviar()
{
	//document.forms.form3.cod_material.value = document.forms.form1.cod_material.value
	document.forms.form1.submit();
}

function carga_div(valor)
{
	
	if(valor == "E")
	{
		//document.getElementById('extruido').style.visibility = "";
		document.getElementById('comprado').style.visibility = "hidden";
		document.getElementById('laminado').style.visibility = "hidden";
		//document.getElementById('extruido').style.position   = "";
	}
	
	if(valor == "C")
	{
		//document.getElementById('extruido').style.visibility = "hidden";
		document.getElementById('comprado').style.visibility = "";
		document.getElementById('laminado').style.visibility = "hidden";
		//document.getElementById('extruido').style.position   = "absolute";
		
	}
	
	if(valor == "L")
	{
		//document.getElementById('extruido').style.visibility = "hidden";
		document.getElementById('comprado').style.visibility = "hidden";
		document.getElementById('laminado').style.visibility = "";
		//document.getElementById('extruido').style.position   = "absolute";
		document.getElementById('comprado').style.position   = "absolute";
	}
}

var indiceFilaFormulario=<?php if($c != "")echo $c+1; else echo "1";?>;
var pct_capa=<?php if($c != "")echo "100"; else echo "0";?>; 
var pct_indice = new Array();

<?php 
$i = 1;
if($acc == "edit" and $_GET['cod_material'] != "")
	while ($porc = mysql_fetch_assoc($porcentaje_capas))
	{
		echo "pct_indice[$i] =".$porc['matc_pct_espesor'].";\n";
		$i++;
	}
?>
function valida_enter(textbox,ev)
{
	if(check_enter(ev))
		addCapa('capas');
}
</script>

<script language="javascript" type="text/javascript">

var indiceFF=1;

function addLamina(nombre){
 var itemId   = document.getElementById("lamina").value;
 var indice   = document.getElementById("lamina").selectedIndex;
 var itemDesc = document.getElementById("lamina").options[indice].text;
 var remove   = '<a href="javascript:removeLamina(this)"><img src="../img/cancel.png" width="16" height="16" border="0" /></a>';
 var hidden   = '<input type="hidden" name="lamina[' + indiceFF + ']" id="lamina" value="' + itemId + '" />';
 
 myNewRow = document.getElementById(nombre).insertRow(-1);
 myNewRow.id=indiceFF;
 myNewCell=myNewRow.insertCell(-1);
 var nuevoSelect="";
 myNewCell.innerHTML=itemDesc + hidden;
 myNewCell=myNewRow.insertCell(-1);
 myNewCell.innerHTML=remove;
 
 
 /*myNewRow = document.getElementById('laminas').insertRow(-1);
 myNewRow.id=indiceFF;
 myNewCell=myNewRow.insertCell(-1);
 myNewCell.innerHTML=itemDesc + hidden;
 myNewCell=myNewRow.insertCell(-1);
 myNewCell.innerHTML=remove;
 myNewCell=myNewRow.insertCell(-1); */
 indiceFF++;
}
 
function removeLamina(obj){
 var nBr = obj;
 while(nBr.nodeName.toLowerCase()!='tr'){
  nBr=nBr.parentNode;
 }
 var root = nBr.parentNode;
 root.removeChild(nBr);
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="mntomateriales.php?acc=save">
<strong>Datos Material</strong>
  <table border="0">
    <tr>
      <td>Cod. Material</td>
      <td>:</td>
      <td><input name="cod_material" type="text" class="default <?php if($cod_material != "") echo "borde_trasparente"; ?>" id="cod_material" maxlength="18" 
	  <?php if($cod_material != "") echo "readonly=\"readonly\" value=\"$cod_material\""; else echo " onkeypress=\"check_key(event,'mntomateriales.php?acc=edit&cod_material=' + this.value)\" value=\"$c_m\""; ?> />
      <a href="javascript:abrir('../browser/materiales.php','cod_material',500,450)"><img src="../img/find.png" width="16" height="16" border="0" /></a></td>
    </tr>
    <tr>
      <td>Descripci&oacute;n</td>
      <td>:</td>
      <td><input name="desc_material" type="text" class="default" id="desc_material" size="45" value="<?php echo $desc_material; ?>" <?php if($cod_material == "") echo "style=\"visibility:hidden\""; ?> autocomplete="off"/></td>
    </tr>
    <tr>
      <td>Tipo</td>
      <td>:</td>
      <td><select name="tipo_material" class="default" id="tipo_material" <?php if($cod_material == "") echo "style=\"visibility:hidden\""; ?> onchange="carga_div(this.value)" >
        <option value="E" <?php if(strtoupper($tipo_material) == "E") echo "selected=\"selected\""; ?>>Film PE Extruido</option>
        <option value="C" <?php if(strtoupper($tipo_material) == "C") echo "selected=\"selected\""; ?>>Film Comprado</option>
        <option value="L" <?php if(strtoupper($tipo_material) == "L") echo "selected=\"selected\""; ?>>Film Laminado</option>
      </select></td>
    </tr>
  </table>
  <script>
try{
	var foc = '<?php echo $cod_material;?>';
	
	if(foc != "")
		document.getElementById('desc_material').focus();
	else
		document.getElementById('cod_material').focus();
}
catch(e)
{
}
</script>
  <br />
  <div id="comprado" style=" <?php if(strtoupper($tipo_material) <> "C") echo "visibility:hidden;"; ?>position:absolute">
    <strong>Comprado</strong>
    <table width="100" border="0">
    <tr>
      <td><select name="part" class="default" id="part">
        <?php echo $row_grupos['code_value'];
do {  
?>
        <option value="<?php echo $row_grupos['code_value']?>" <?php if($row_comp['matco_pt_part_type'] == $row_grupos['code_value']) echo "selected=\"selected\""; ?> ><?php echo $row_grupos['code_cmmt']?></option>
        <?php
} while ($row_grupos = mysql_fetch_assoc($grupos));
/*  $rows = mysql_num_rows($articulos);
  if($rows > 0) {
      mysql_data_seek($articulos, 0);
	  $row_articulos = mysql_fetch_assoc($articulos); 
  } */
?>
      </select></td>
      </tr>
  </table>
  </div>
  <div id="laminado" style=" <?php if(strtoupper($tipo_material) <> "L") echo "visibility:hidden;"; ?>">
  <strong>Laminado</strong>
  <table border="0" id="laminas">
    <tr>
      <td width="62">L&aacute;mina 1</td>
      <td>:</td>
      <td><select name="lamina1" class="default" id="lamina1">
        <?php
do {  
?>
        <option value="<?php echo $row_materiales['mat_id']?>" <?php if($lam[0] == $row_materiales['mat_id'])echo "selected=\"selected\""; ?>><?php echo $row_materiales['mat_id']." - ";?><?php echo $row_materiales['mat_desc'];?></option>
        <?php
} while ($row_materiales = mysql_fetch_assoc($materiales));
  $rows = mysql_num_rows($materiales);
  if($rows > 0) {
      mysql_data_seek($materiales, 0);
	  $row_materiales = mysql_fetch_assoc($materiales);
  }
?>
      </select></td>
      </tr>
    <tr>
      <td>L&aacute;mina 2</td>
      <td>:</td>
      <td><select name="lamina2" class="default" id="lamina2">
        <?php
do {  
?>
        <option value="<?php echo $row_materiales['mat_id']?>" <?php if($lam[1] == $row_materiales['mat_id'])echo "selected=\"selected\""; ?>><?php echo $row_materiales['mat_id']." - ";?><?php echo $row_materiales['mat_desc'];?></option>
        <?php
} while ($row_materiales = mysql_fetch_assoc($materiales));
  $rows = mysql_num_rows($materiales);
  if($rows > 0) {
      mysql_data_seek($materiales, 0);
	  $row_materiales = mysql_fetch_assoc($materiales);
  }
?>
      </select></td>
    </tr>
    <tr>
      <td>L&aacute;mina 3</td>
      <td>:</td>
      <td><select name="lamina3" class="default" id="lamina3">
      	<option value="">No Aplica</option>
        <?php
do {  
?>
        <option value="<?php echo $row_materiales['mat_id']?>" <?php if($lam[2] == $row_materiales['mat_id'])echo "selected=\"selected\""; ?>><?php echo $row_materiales['mat_id']." - ";?><?php echo $row_materiales['mat_desc'];?></option>
        <?php
} while ($row_materiales = mysql_fetch_assoc($materiales));
  $rows = mysql_num_rows($materiales);
  if($rows > 0) {
      mysql_data_seek($materiales, 0);
	  $row_materiales = mysql_fetch_assoc($materiales);
  }
?>
      </select></td>
    </tr>
    <tr>
      <td>L&aacute;mina 4</td>
      <td>:</td>
      <td><select name="lamina4" class="default" id="lamina4">
        <option value="">No Aplica</option>
        <?php
do {  
?>
        <option value="<?php echo $row_materiales['mat_id']?>" <?php if($lam[3] == $row_materiales['mat_id'])echo "selected=\"selected\""; ?>><?php echo $row_materiales['mat_id']." - ";?><?php echo $row_materiales['mat_desc'];?></option>
        <?php
} while ($row_materiales = mysql_fetch_assoc($materiales));
  $rows = mysql_num_rows($materiales);
  if($rows > 0) {
      mysql_data_seek($materiales, 0);
	  $row_materiales = mysql_fetch_assoc($materiales);
  }
?>
      </select></td>
    </tr>
  </table>
  </div>
</form>
<script>
		var flag = '<?php echo $acc;?>';

		if(flag != '')
			document.getElementById('desc_material').focus();
	</script>
  <br />
  <br />
<br />
  <table width="479" border="0">
    <tr>
      <td width="272" align="left" class="descripcion_alpie" ><?php if($acc == "edit" and $totalRows == 0 and $_GET['cod_material'] != "")echo $text_newreg; if($acc == "edit" and $totalRows == 1)echo $text_editreg; if($acc == "edit" and $_GET['cod_material'] == "") echo $text_blank_generico; ?></td>
      <td width="86" align="right"><?php if ($id_usuario == ""){?>
        <input name="button" type="button" onclick="window.open('mntomateriales.php?acc=edit','_self');" class="default" id="button" value="Editar" />
        <?php }?></td>
      <td width="46" align="right"><input name="button2" type="button" onclick="window.open('mntomateriales.php?cm=<?php echo $cod_material;?>','_self');" class="default" id="button2" value="Volver" /></td>
      <td width="57" align="right"><input id="sig" name="Submit" type="button" class="default" value="Guardar" <?php if($tipo_material == "") echo "disabled=\"disabled\""; ?>  onclick="enviar();" /></td>
    </tr>
</table>
</body>
</html>
<?php

mysql_free_result($materiales);
?>
<?php 
mysql_close($cotiza);
?>